package swingSection;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import connection.Connector;
import dataAccessLayer.DataManipulator;

import entities.User;

public class MainModule extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPasswordField pass = new JPasswordField();
	private JLabel passLabel = new JLabel();
	private JButton passButton = new JButton();
	private JTable userTable = new JTable(getMyTableModel());
	private JButton addButton = new JButton();
	private JButton editButton = new JButton();
	private JButton deleteButton = new JButton();
	private JButton refreshButton=new JButton();
	private JLabel nameColumn = new JLabel();
	private JLabel passColumn = new JLabel();
	private MyTableModel myTableModel;
	private AddDialog addDialog = new AddDialog();
	private EditDialog editDialog = new EditDialog();
	private DeleteDialog deleteDialog = new DeleteDialog();
	
	public MainModule() {
		super();
		jContentPane = new JPanel();
		jContentPane.setLayout(null);
		this.add(jContentPane);
		this.setTitle("Foto Album - Administration");
		this.setLayout(null);
		this.setBounds(new Rectangle(262, 199, 500, 370));
		passLabel.setBounds(new Rectangle(10, 10, 160, 25));
		passLabel.setText("Enter password:");
		this.add(passLabel);
		nameColumn.setBounds(new Rectangle(50, 45, 100, 25));
		nameColumn.setText("Username");
		nameColumn.setVisible(false);
		this.add(nameColumn);
		passColumn.setBounds(new Rectangle(250, 45, 100, 25));
		passColumn.setText("Password");
		passColumn.setVisible(false);
		this.add(passColumn);
		userTable.setBounds(new Rectangle(50, 70, 400, 200));
		userTable.setVisible(false);
		this.add(userTable);
		addButton.setBounds(new Rectangle(50, 290, 90, 25));
		editButton.setBounds(new Rectangle(150, 290, 90, 25));
		deleteButton.setBounds(new Rectangle(250, 290, 90, 25));
		refreshButton.setBounds(new Rectangle(350,290,100,25));
		addButton.setText("Add");
		editButton.setText("Edit");
		deleteButton.setText("Delete");
		refreshButton.setText("Refresh");
		addButton.setVisible(false);
		editButton.setVisible(false);
		deleteButton.setVisible(false);
		refreshButton.setVisible(false);
		this.add(addButton);
		this.add(editButton);
		this.add(deleteButton);
		this.add(refreshButton);
		passButton.setBounds(new Rectangle(280, 10, 100, 25));
		passButton.setText("GO");
		this.getRootPane().setDefaultButton(passButton);
		final char[] password=new char[]{'f','o','t','o','0','9','0','1'};
		passButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (Arrays.equals(pass.getPassword(), password)) {
					pass.setVisible(false);
					passLabel.setVisible(false);
					passButton.setVisible(false);
					userTable.setVisible(true);
					loadUsers();
					nameColumn.setVisible(true);
					passColumn.setVisible(true);
					addButton.setVisible(true);
					addButton.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {
							int result = addDialog.showDialog();
							if (result == 0) {
								addUser();
							}
						}
					});
					editButton.setVisible(true);
					editButton.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {
							int result = editDialog.showDialog();
							if (result == 0) {
								editUser();
							}
						}
					});
					deleteButton.setVisible(true);
					deleteButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e) {
							int result = deleteDialog.showDialog();
							if (result == 0) {
								deleteUser();
							}

						}

					});
					refreshButton.setVisible(true);
					refreshButton.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent e) {
							loadUsers();
							
						}
						
					});
				} else {
					passLabel.setText("Invalid password!");
					pass.setText("");
				}
			}
		});
		this.add(passButton);
		pass.setBounds(new Rectangle(170, 10, 100, 25));
		this.add(pass);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public MyTableModel getMyTableModel() {
		if (myTableModel == null) {
			myTableModel = new MyTableModel();
		}
		return myTableModel;
	}

	public void loadUsers() {
		String hql = "from User";
		Connector.startNewSession();
		List<User> users = Connector.getSession().createQuery(hql)
				.list();
		if (users != null) {
			getMyTableModel().clear();
			getMyTableModel().addUsers(users);
		} else {
			System.out.println("error");
		}
	}

	public void addUser() {
		String userName = addDialog.getUserName();
		String password = addDialog.getPassword();
		DataManipulator dm = new DataManipulator();
		dm.createNewUser(userName, password);
		loadUsers();
	}

	public void editUser() {
		String userName = editDialog.getUserName();
		String newUserName = editDialog.getNewUserName();
		String newUserPassword = editDialog.getNewPassword();
		DataManipulator dm = new DataManipulator();
		dm.editUser(userName, newUserName, newUserPassword);
		loadUsers();
	}

	public void deleteUser() {
		String userName = deleteDialog.getUserName();
		DataManipulator dm = new DataManipulator();
		dm.deleteUser(userName);
		loadUsers();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainModule mm = new MainModule();
				mm.setVisible(true);
			}
		});

	}

}
